Skip to main content

Disk Management va Partitioning

Disk management va partitioning - bu Linux system administrator uchun fundamental skill'lardan biri. Bu mavzu storage planning, system expansion va data management uchun juda muhim.

Disk va Storage Asoslari

Storage Types

# Hard Disk Types:
# HDD (Hard Disk Drive) - Traditional spinning disks
# SSD (Solid State Drive) - Flash-based storage
# NVMe (Non-Volatile Memory Express) - High-speed SSD interface
# SATA, IDE, SCSI - Interface types

# Virtual Storage:
# LVM (Logical Volume Manager) - Flexible volume management
# RAID (Redundant Array of Independent Disks) - Redundancy and performance
# Network Storage - NFS, iSCSI, Ceph

Device Naming Convention

# SATA/SCSI devices
/dev/sda, /dev/sdb, /dev/sdc # First, second, third disk
/dev/sda1, /dev/sda2 # Partitions on first disk

# NVMe devices
/dev/nvme0n1, /dev/nvme0n1p1 # NVMe disk and partition

# IDE devices (legacy)
/dev/hda, /dev/hdb # Primary master, primary slave

# Virtual devices
/dev/mapper/vg-lv # LVM logical volume
/dev/md0, /dev/md1 # Software RAID devices

# Loop devices
/dev/loop0, /dev/loop1 # Loopback devices

Disk Information Commands

Basic Disk Information

# List all block devices
lsblk # Tree view of block devices
lsblk -f # Include filesystem information
lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT,UUID # Custom columns

# Disk usage
df -h # Filesystem usage (human readable)
df -i # Inode usage
du -sh /path/to/directory # Directory size
du -sh /* 2>/dev/null # Top-level directory sizes

# Block device information
blkid # UUID and filesystem types
blkid /dev/sda1 # Specific device info
sudo file -s /dev/sda1 # File system type

# Detailed device information
sudo fdisk -l # List all disks and partitions
sudo fdisk -l /dev/sda # Specific disk
sudo parted -l # GNU parted list
sudo lshw -class disk # Hardware information

Advanced Disk Information

# Disk geometry and details
sudo hdparm -i /dev/sda # IDE/SATA drive information
sudo hdparm -I /dev/sda # Detailed drive information
sudo smartctl -a /dev/sda # SMART information (install smartmontools)
sudo smartctl -H /dev/sda # Health status

# Partition table information
sudo gdisk -l /dev/sda # GPT partition table
sudo sfdisk -l /dev/sda # Another partitioning tool

# I/O statistics
iostat # I/O statistics
iostat -x 1 # Extended stats, 1 second intervals
iotop # I/O usage by process (sudo apt install iotop)

Partitioning Tools

fdisk (MBR Partitions)

# Start fdisk
sudo fdisk /dev/sda # Interactive partitioning

# fdisk commands:
# p - print partition table
# n - create new partition
# d - delete partition
# t - change partition type
# a - toggle bootable flag
# w - write changes and exit
# q - quit without saving

# Example session:
sudo fdisk /dev/sdb
> n # New partition
> p # Primary partition
> 1 # Partition number
> <Enter> # First sector (default)
> +10G # Size (10 GB)
> w # Write changes

gdisk (GPT Partitions)

# Start gdisk for GPT
sudo gdisk /dev/sda

# gdisk commands:
# p - print partition table
# n - create new partition
# d - delete partition
# t - change partition type
# x - expert mode
# w - write changes and exit
# q - quit without saving

# GPT advantages:
# - Supports disks > 2TB
# - More than 4 primary partitions
# - Better data integrity
# - UEFI compatibility

parted (Universal Tool)

# Interactive mode
sudo parted /dev/sda

# parted commands:
# print - show partition table
# mklabel gpt - create GPT partition table
# mklabel msdos - create MBR partition table
# mkpart - create partition
# rm - remove partition
# quit - exit

# Non-interactive examples:
sudo parted /dev/sdb mklabel gpt # Create GPT table
sudo parted /dev/sdb mkpart primary ext4 0% 50% # Create partition
sudo parted /dev/sdb mkpart primary ext4 50% 100% # Second partition

Automated Partitioning Script

#!/bin/bash
# partition-disk.sh - Automated disk partitioning

DEVICE="/dev/sdb"
PARTITION_SCHEME="gpt"

# Safety check
if [ ! -b "$DEVICE" ]; then
echo "Error: $DEVICE is not a block device"
exit 1
fi

# Warning
echo "WARNING: This will destroy all data on $DEVICE"
read -p "Continue? (yes/no): " confirm

if [ "$confirm" != "yes" ]; then
echo "Aborted"
exit 0
fi

# Create partition table
echo "Creating $PARTITION_SCHEME partition table on $DEVICE"
sudo parted "$DEVICE" mklabel "$PARTITION_SCHEME"

# Create partitions
echo "Creating partitions..."

# Boot partition (512MB)
sudo parted "$DEVICE" mkpart primary fat32 1MiB 513MiB
sudo parted "$DEVICE" set 1 boot on

# Root partition (20GB)
sudo parted "$DEVICE" mkpart primary ext4 513MiB 20.5GiB

# Home partition (remaining space)
sudo parted "$DEVICE" mkpart primary ext4 20.5GiB 100%

# Show result
echo "Partition table created:"
sudo parted "$DEVICE" print

echo "Next steps:"
echo "1. Format partitions: mkfs.ext4 /dev/sdb2"
echo "2. Mount partitions: mount /dev/sdb2 /mnt"
echo "3. Update /etc/fstab if needed"

Filesystem Creation

Common Filesystem Types

# ext4 - Default Linux filesystem
sudo mkfs.ext4 /dev/sdb1
sudo mkfs.ext4 -L "MyData" /dev/sdb1 # With label

# XFS - High-performance filesystem
sudo mkfs.xfs /dev/sdb1
sudo mkfs.xfs -f /dev/sdb1 # Force overwrite

# Btrfs - Advanced filesystem with snapshots
sudo mkfs.btrfs /dev/sdb1
sudo mkfs.btrfs -L "BtrfsVolume" /dev/sdb1 # With label

# FAT32 - Windows compatibility
sudo mkfs.vfat -F 32 /dev/sdb1

# NTFS - Windows filesystem
sudo mkfs.ntfs /dev/sdb1

# Swap partition
sudo mkswap /dev/sdb2
sudo swapon /dev/sdb2 # Activate swap

Filesystem Options

# ext4 options
sudo mkfs.ext4 -b 4096 /dev/sdb1 # Block size
sudo mkfs.ext4 -m 1 /dev/sdb1 # Reserved space (1%)
sudo mkfs.ext4 -j /dev/sdb1 # With journal
sudo mkfs.ext4 -O ^has_journal /dev/sdb1 # Without journal

# XFS options
sudo mkfs.xfs -b size=4096 /dev/sdb1 # Block size
sudo mkfs.xfs -s size=512 /dev/sdb1 # Sector size
sudo mkfs.xfs -d agcount=4 /dev/sdb1 # Allocation groups

# Check filesystem after creation
sudo fsck.ext4 /dev/sdb1
sudo xfs_check /dev/sdb1

LVM (Logical Volume Manager)

LVM Concepts

# Physical Volume (PV) - Physical disk or partition
# Volume Group (VG) - Collection of PVs
# Logical Volume (LV) - Virtual partition from VG
#
# Advantages:
# - Dynamic resizing
# - Snapshots
# - Striping and mirroring
# - Easy backup

LVM Setup

# Install LVM tools
sudo apt install lvm2 # Ubuntu/Debian
sudo yum install lvm2 # CentOS/RHEL

# Create Physical Volume
sudo pvcreate /dev/sdb1
sudo pvcreate /dev/sdb2
sudo pvdisplay # Show PVs

# Create Volume Group
sudo vgcreate mydata /dev/sdb1 /dev/sdb2
sudo vgdisplay # Show VGs
sudo vgs # Short format

# Create Logical Volume
sudo lvcreate -L 10G -n volume1 mydata # 10GB volume
sudo lvcreate -l 50%VG -n volume2 mydata # 50% of VG
sudo lvdisplay # Show LVs
sudo lvs # Short format

# Format and mount
sudo mkfs.ext4 /dev/mydata/volume1
sudo mkdir /mnt/volume1
sudo mount /dev/mydata/volume1 /mnt/volume1

LVM Management

# Extend Volume Group
sudo pvcreate /dev/sdb3
sudo vgextend mydata /dev/sdb3

# Extend Logical Volume
sudo lvextend -L +5G /dev/mydata/volume1 # Add 5GB
sudo lvextend -l +100%FREE /dev/mydata/volume1 # Use all free space

# Resize filesystem (after extending LV)
sudo resize2fs /dev/mydata/volume1 # ext2/3/4
sudo xfs_growfs /mnt/volume1 # XFS

# Reduce Logical Volume (ext2/3/4 only)
# DANGER: Always backup data first!
sudo umount /dev/mydata/volume1
sudo e2fsck -f /dev/mydata/volume1
sudo resize2fs /dev/mydata/volume1 8G # Resize filesystem first
sudo lvreduce -L 8G /dev/mydata/volume1 # Then reduce LV

# Remove Logical Volume
sudo umount /dev/mydata/volume1
sudo lvremove /dev/mydata/volume1

# Remove Volume Group
sudo vgremove mydata

# Remove Physical Volume
sudo pvremove /dev/sdb1

LVM Snapshots

# Create snapshot
sudo lvcreate -L 1G -s -n volume1-snap /dev/mydata/volume1

# Mount snapshot
sudo mkdir /mnt/snapshot
sudo mount /dev/mydata/volume1-snap /mnt/snapshot

# Restore from snapshot
sudo umount /dev/mydata/volume1
sudo umount /dev/mydata/volume1-snap
sudo lvconvert --merge /dev/mydata/volume1-snap

# Remove snapshot
sudo umount /dev/mydata/volume1-snap
sudo lvremove /dev/mydata/volume1-snap

RAID Configuration

Software RAID (mdadm)

# Install mdadm
sudo apt install mdadm # Ubuntu/Debian
sudo yum install mdadm # CentOS/RHEL

# RAID Levels:
# RAID 0 - Striping (performance, no redundancy)
# RAID 1 - Mirroring (redundancy)
# RAID 5 - Striping with parity (3+ disks)
# RAID 6 - Striping with double parity (4+ disks)
# RAID 10 - Striped mirrors (4+ disks)

# Create RAID 1 (mirror)
sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdb2

# Create RAID 5
sudo mdadm --create --verbose /dev/md1 --level=5 --raid-devices=3 /dev/sdc1 /dev/sdc2 /dev/sdc3

# Check RAID status
cat /proc/mdstat
sudo mdadm --detail /dev/md0

# Save RAID configuration
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
sudo update-initramfs -u

RAID Management

# Monitor RAID
sudo mdadm --monitor /dev/md0 # Monitor for failures

# Add spare disk
sudo mdadm --add /dev/md0 /dev/sdb3

# Remove failed disk
sudo mdadm --fail /dev/md0 /dev/sdb2
sudo mdadm --remove /dev/md0 /dev/sdb2

# Replace disk
sudo mdadm --add /dev/md0 /dev/sdb4 # Add new disk
# RAID will automatically rebuild

# Stop RAID
sudo umount /dev/md0
sudo mdadm --stop /dev/md0

# Destroy RAID
sudo mdadm --zero-superblock /dev/sdb1 /dev/sdb2

Disk Monitoring and Maintenance

Health Monitoring

# SMART monitoring
sudo smartctl -a /dev/sda # All SMART information
sudo smartctl -H /dev/sda # Health status
sudo smartctl -t short /dev/sda # Short self-test
sudo smartctl -t long /dev/sda # Long self-test
sudo smartctl -l selftest /dev/sda # Self-test results

# Continuous monitoring
sudo smartd # SMART daemon
cat /etc/smartd.conf # Configuration file

# Disk temperature
sudo hddtemp /dev/sda # Temperature monitoring

Performance Testing

# Read performance test
sudo hdparm -t /dev/sda # Buffered disk reads
sudo hdparm -T /dev/sda # Cache reads

# dd benchmark
sudo dd if=/dev/zero of=/tmp/testfile bs=1M count=1024 conv=fdatasync
sudo dd if=/tmp/testfile of=/dev/null bs=1M count=1024

# iozone benchmark (install iozone3)
iozone -a # Comprehensive test

# fio benchmark (install fio)
fio --name=random-read --ioengine=posixaio --rw=randread --bs=4k --numjobs=1 --size=4g --iodepth=1 --runtime=60 --time_based --end_fsync=1

Disk Cleanup and Maintenance

#!/bin/bash
# disk-maintenance.sh - Comprehensive disk maintenance

LOG_FILE="/var/log/disk-maintenance.log"

log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}

# Check disk space
check_disk_space() {
log_message "=== Disk Space Check ==="

df -h | while read filesystem size used avail percent mountpoint; do
if [[ "$percent" =~ ^[0-9]+% ]]; then
percent_num=$(echo "$percent" | sed 's/%//')
if [ "$percent_num" -gt 90 ]; then
log_message "CRITICAL: $mountpoint is ${percent} full"
elif [ "$percent_num" -gt 80 ]; then
log_message "WARNING: $mountpoint is ${percent} full"
fi
fi
done
}

# Find large files
find_large_files() {
log_message "=== Large Files (>1GB) ==="
find / -type f -size +1G -exec ls -lh {} \; 2>/dev/null | head -20 | while read file; do
log_message "$file"
done
}

# Clean temporary files
clean_temp_files() {
log_message "=== Cleaning Temporary Files ==="

# Clean /tmp
files_removed=$(find /tmp -type f -mtime +7 -delete -print 2>/dev/null | wc -l)
log_message "Removed $files_removed old files from /tmp"

# Clean /var/tmp
files_removed=$(find /var/tmp -type f -mtime +30 -delete -print 2>/dev/null | wc -l)
log_message "Removed $files_removed old files from /var/tmp"

# Clean package cache
if command -v apt >/dev/null; then
apt autoclean >> "$LOG_FILE" 2>&1
log_message "Cleaned APT cache"
fi

# Clean journal logs
journalctl --vacuum-time=30d >> "$LOG_FILE" 2>&1
log_message "Cleaned old journal logs"
}

# Check filesystem integrity
check_filesystem() {
log_message "=== Filesystem Check ==="

# Check mounted filesystems (read-only check)
mount | grep "^/dev" | awk '{print $1}' | while read device; do
filesystem_type=$(blkid -o value -s TYPE "$device" 2>/dev/null)
case "$filesystem_type" in
"ext2"|"ext3"|"ext4")
tune2fs_output=$(tune2fs -l "$device" 2>/dev/null | grep "Last checked")
log_message "$device ($filesystem_type): $tune2fs_output"
;;
"xfs")
log_message "$device ($filesystem_type): XFS filesystem"
;;
esac
done
}

# SMART health check
check_smart_health() {
log_message "=== SMART Health Check ==="

# Find all disks
ls /dev/sd* /dev/nvme* 2>/dev/null | grep -E "(sd[a-z]$|nvme[0-9]+n[0-9]+$)" | while read disk; do
if smartctl -H "$disk" >/dev/null 2>&1; then
health=$(smartctl -H "$disk" | grep "SMART overall-health" | awk '{print $6}')
log_message "$disk: SMART health - $health"

# Check temperature
temp=$(smartctl -a "$disk" | grep -E "Temperature|Airflow_Temperature" | head -1 | awk '{print $10}')
if [[ "$temp" =~ ^[0-9]+$ ]]; then
log_message "$disk: Temperature - ${temp}°C"
fi
fi
done
}

# Main execution
log_message "Starting disk maintenance..."
check_disk_space
find_large_files
clean_temp_files
check_filesystem
check_smart_health
log_message "Disk maintenance completed"

# Email report if mail is available
if command -v mail >/dev/null 2>&1; then
tail -50 "$LOG_FILE" | mail -s "Disk Maintenance Report - $(hostname)" admin@company.com
fi

Emergency Disk Recovery

Filesystem Recovery

# Unmount filesystem first
sudo umount /dev/sdb1

# Check and repair ext2/3/4
sudo fsck.ext4 -f /dev/sdb1 # Force check
sudo fsck.ext4 -y /dev/sdb1 # Auto-repair

# Check XFS
sudo xfs_check /dev/sdb1 # Check only
sudo xfs_repair /dev/sdb1 # Repair

# Check Btrfs
sudo btrfs check /dev/sdb1 # Check only
sudo btrfs check --repair /dev/sdb1 # Repair

# Recover deleted files
sudo apt install testdisk photorec # Install recovery tools
sudo photorec /dev/sdb1 # File recovery
sudo testdisk /dev/sdb # Partition recovery

Data Recovery

# dd_rescue for damaged disks
sudo apt install ddrescue
sudo ddrescue /dev/sdb1 /recovery/sdb1.img /recovery/sdb1.log

# Create image of failing disk
sudo dd if=/dev/sdb of=/recovery/sdb.img bs=1M conv=noerror,sync

# Mount image for data recovery
sudo mkdir /mnt/recovery
sudo mount -o loop /recovery/sdb1.img /mnt/recovery

# Scan for filesystem signatures
sudo blkid /recovery/sdb.img

# Advanced recovery tools
sudo apt install foremost scalpel sleuthkit

Partition Recovery

# TestDisk for partition recovery
sudo testdisk /dev/sdb

# Recreate partition table manually
sudo fdisk /dev/sdb
# Use 'p' to try to read existing partitions
# Use 'n' to recreate partitions with same start/end sectors

# Backup partition table
sudo sfdisk -d /dev/sda > partition-backup.txt
# Restore partition table
sudo sfdisk /dev/sda < partition-backup.txt

Bu tutorial disk management va partitioning bo'yicha comprehensive ma'lumot beradi - basic disk operations'dan tortib advanced LVM va RAID configuration'lari, monitoring va recovery techniques bilan birga.